﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdCopyGRepToDS_PGE.h"
#include "IUiView.h"
#include "IElement.h"
#include "OwnerPtr.h"
#include "IDocument.h"
#include "ElementId.h"
#include "ISelection.h"
#include "IUiDocument.h"
#include "IDirectShape.h"
#include "UiCommonDialog.h"
#include "GcmpCommandState.h"
#include "IUserTransaction.h"
#include "CommandParameters.h"
#include "IElementModelShape.h"
#include "IPureGraphicsElement.h"
#include "IGraphicsElementShape.h"
#include "IUiDocumentViewManager.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "IElementViewSpecificShapeComponent.h"

#include "GRepCommandIds.h"
#include "CommandRegister.h"
#include "CmdGRepModuleEntry.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

std::wstring PARAM_ID_CmdCopyGRepToDS_PGE = L"isViewGRep";

REGISTER_COMMAND(CmdCopyGRepToDS_PGE)

CmdCopyGRepToDS_PGE::CmdCopyGRepToDS_PGE()
    :CommandBase(ID_CMD_GREP_CONVERT_TO_DS)
{
}

bool CmdCopyGRepToDS_PGE::IsEnabled() const
{
    // 需要在项目环境中，并且有图元被选中
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false;
    }
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() == 1)
        {
            return true;
        }
    }
    return false;
}

bool CmdCopyGRepToDS_PGE::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

// 这个命令同时包含用选中图元的模型GRep或视图GRep创建IDirectShape/IPureGraphicsElement。会通过命令参数控制
gcmp::OwnerPtr<gcmp::IAction> CmdCopyGRepToDS_PGE::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    return CmdCopyGRepToDS_PGE::CopyGRepToDS_PGE(false);
}

gcmp::OwnerPtr<gcmp::IAction> Sample::CmdCopyGRepToDS_PGE::CopyGRepToDS_PGE(bool isViewGRep)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"获取UI文档视图管理器失败",L"GDMP",L"2024-03-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"获取UI文档失败",L"GDMP",L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"获取文档失败",L"GDMP",L"2024-03-30");
    IUiView* pCurrentView = pUiDocViewMgr->GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMP",L"2024-03-30");
    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMP",L"2024-03-30");

    // 获得选择图元
    ISelection* pSelection = ISelection::Get();
    std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(elemMaps.size() > 0, L"elemMaps为空",L"GDMP",L"2024-03-30");

    ElementId id = *elemMaps.begin();
    IElement* pElement = pDoc->GetElement(id);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElement, L"pElement为空",L"GDMP",L"2024-03-30");

    OwnerPtr<IGraphicsElementShape> opSourceGRep; // 用于缓存pSourceGRep相关的OwnerPtr，避免被释放
    const IGraphicsElementShape* pSourceGRep = nullptr;

    if (isViewGRep)
    {
        // 获得图元的视图GRep组件
        const IElementViewSpecificShapeComponent* pElemViewComponent = pElement->GetViewSpecificShapeComponent();
        if (nullptr == pElemViewComponent)
        {
            UiCommonDialog::ShowMessageBox(L"提示", L"选择的图元没有IElementViewSpecificShapeComponent，因此无法生成IDirectShape", (int)UiCommonDialog::ButtonType::Yes);
            return nullptr;
        }
        // 从视图GRep组件生成当前视图的视图GRep
        opSourceGRep = pElemViewComponent->CreateViewSpecificElementShape(pModelView);
        if (nullptr == opSourceGRep)
        {
            UiCommonDialog::ShowMessageBox(L"提示", L"选择的图元模型图形表达中IGraphicsElementShape为空，因此无法生成IDirectShape", (int)UiCommonDialog::ButtonType::Yes);
            return nullptr;
        }
        pSourceGRep = opSourceGRep.get();
    }
    else
    {
        // 获得图元的模型GRep组件
        IElementModelShape* pElementModelShape = pElement->GetElementModelShape();
        if (nullptr == pElementModelShape)
        {
            UiCommonDialog::ShowMessageBox(L"提示", L"选择的图元没有IElementModelShape，因此无法生成IDirectShape", (int)UiCommonDialog::ButtonType::Yes);
            return nullptr;
        }
        // 从模型GRep组件获得模型GRep
        pSourceGRep = pElementModelShape->GetGraphicsElementShape();
        if (nullptr == pSourceGRep)
        {
            UiCommonDialog::ShowMessageBox(L"提示", L"选择的图元模型图形表达中IGraphicsElementShape为空，因此无法生成IDirectShape", (int)UiCommonDialog::ButtonType::Yes);
            return nullptr;
        }
    }

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, L"从选择图元的几何创建DirectShape");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTransaction, L"opTransaction为空",L"GDMP",L"2024-03-30");

    OwnerPtr<IGraphicsElementShape> opGraphicsElementShape = IGraphicsElementShape::Create(pSourceGRep);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGraphicsElementShape, L"opGraphicsElementShape为空",L"GDMP",L"2024-03-30");

    // 根据UI选项创建IDirectShape或IPureGraphicsElement
    if (CmdGRepModuleEntry::GetConvertStyle() == ConvertStyle::DirectShape)
    {
        IDirectShape* pDirectShape = IDirectShape::Create(pDoc, BuiltInCategoryUniIdentities::BICU_FORM);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空",L"GDMP",L"2024-03-30");
        pDirectShape->SetGraphicsElementShape(TransferOwnership(opGraphicsElementShape));
    }
    else
    {
        IPureGraphicsElement* pPGE = IPureGraphicsElement::Create(pDoc, TransferOwnership(opGraphicsElementShape));
    }

    opTransaction->Commit();
    return nullptr;
}

#pragma region 将视图GRep复制创建为DirectShape或IPureGraphicsElement
REGISTER_COMMAND(CmdViewGRepCopyToDS_PGE)

CmdViewGRepCopyToDS_PGE::CmdViewGRepCopyToDS_PGE()
    :CommandBase(ID_CMD_VIEW_GREP_CONVERT_TO_DS)
{
}

bool CmdViewGRepCopyToDS_PGE::IsEnabled() const
{
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false;
    }
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() == 1)
        {
            return true;
        }
    }
    return false;
}

bool CmdViewGRepCopyToDS_PGE::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

gcmp::OwnerPtr<gcmp::IAction> Sample::CmdViewGRepCopyToDS_PGE::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    // 参数中传递使用视图GRep
    return CmdCopyGRepToDS_PGE::CopyGRepToDS_PGE(true);
}
#pragma endregion

